package com.holly.common_view.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;


/**
 * 类似侧拉删除
 *
 * @author Joey Huang（QQ:273179370）
 * @created 2016/6/27
 */
public class SlideDeleteView extends HorizontalScrollView {
    /**
     * 左侧
     * 容器第一个view 默认为 left
     */
    private ViewGroup mLeftContent;

    /**
     * 中间内容
     * view 初始化过程中，必须设置中间内容为 parent width
     *
     */
    private ViewGroup mContent;

    /**
     * 左侧宽度
     */
    private int mLeftWidth;

    /**
     * 监听
     */
    private OnStateChangedListener mListener;

    /**
     * 标记第一次测量
     */
    private boolean once;

    /**
     * 标记状态 左侧是否打开
     */
    private boolean isOpen;

    public SlideDeleteView(Context context) {
        this(context,null);
    }

    public SlideDeleteView(Context context, AttributeSet attrs) {
        this(context,attrs,0);
    }

    public SlideDeleteView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.setOverScrollMode(OVER_SCROLL_NEVER);
        once=true;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (once&&getChildCount()>0){
            ViewGroup childAt = (ViewGroup) getChildAt(0);
            if (childAt.getChildCount()>=2){
                mLeftContent= (ViewGroup) childAt.getChildAt(0);
                once=false;
                isOpen=true;
            }
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (changed&&isOpen&&mLeftContent!=null){
            mLeftWidth =mLeftContent.getWidth();
            close();
        }
    }
    private int action ;
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        action = ev.getAction();
        switch (action){
            case MotionEvent.ACTION_UP:
                changedScrollX();
                return true;
        }
        return super.onTouchEvent(ev);
    }

    private int mScrollx=0;
    /**
     * 根据滑动位置，设置滑动状态  open or close
     */
    protected void changedScrollX(){
        mScrollx= getScrollX();
        if (mScrollx<= mLeftWidth /2){
           open();
        }else {
            close();
        }
    }

    /**
     * 关闭
     */
    public void close(){
        this.smoothScrollTo(mLeftWidth,0);
        isOpen=false;
        if (mListener != null) {
            mListener.stateChanged(isOpen);
        }
    }

    /**
     * 打开
     */
    public void open(){
        this.smoothScrollTo(0,0);
        isOpen=true;
        try {
            if (mListener != null) {
                mListener.stateChanged(isOpen);
            }
        }catch (ClassCastException e){
            e.printStackTrace();
        }
    }

    public boolean isOpen() {
        return isOpen;
    }

    public void setOnStateChangedListener(OnStateChangedListener listener){
        mListener=listener;
    }

    public interface OnStateChangedListener {
        void stateChanged(boolean isOpen);
    }
}
